#!/bin/bash
MAKE="make"
export MAKE
HDD_IMAGE="hdd.img"
KRNL_IMAGE="kernel"
KRNL_ZIP="kernel.gz"
MYOS_SRC=`pwd`"/src"
OS_PATH=`pwd`
export MYOS_SRC

echo "Compiling MyOs...in "${MYOS_SRC}
cd src
${MAKE} $1
cd ../
echo "Ok."

if [ $1 ]; then
    exit 1
fi

echo "Configuring image..."

if [ ! -d ${OS_PATH}/hdd ]; then
    mkdir ${OS_PATH}/hdd
fi

echo -n "Mounting HDD image..."
sudo mount -t vfat -o loop,offset=32256 ${OS_PATH}/img/hdd.img ${OS_PATH}/hdd
echo "Ok."

echo "Copying OS..."
if [ ! -d ${OS_PATH}/hdd/sys ]; then
    mkdir ${OS_PATH}/hdd/sys
fi
sudo rm -f ${MYOS_SRC}/"sys/"${KRNL_ZIP}
sudo gzip ${MYOS_SRC}"/sys/"${KRNL_IMAGE}
sudo cp -f ${MYOS_SRC}"/sys/"${KRNL_ZIP} ${OS_PATH}"/hdd/sys/"${KRNL_ZIP}
sudo umount -df ${OS_PATH}/hdd

echo "Done."
#unset $MYOS_SRC
#unset $MAKE
